MergeIntersections<T>
&で結合した型を読みやすくする
単純な定義
Pick<T, K extends keyof T>を使う
code:ts
type MergeIntersections<T> = Pick<T, keyof T>;
定義だけ見ると何もやってないように見えるが、実際は挙動が変わるmrsekut.icon
{....A, ...B} と & を区別するの本質を表した定義という感じがする
よりhackyな書き方
code:ts
type MergeIntersections<T> = Omit<T, never>;
定義
code:ts
type MergeIntersections<T> = T extends object
? { K in keyof T: MergeIntersections<TK> }
: T;
ネストなども加味する
intersectionした後の加工なので当然だが、propertyが競合してるとneverになることに注意
これは、MergeIntersections<T>の注意ではなく、&の注意
code:ts
type Foo = { a: number; b: string };
type Bar = { b: number; c: boolean };
// { a: number; b: never; c: boolean; }
type A = MergeIntersections<Foo & Bar>;
type-challengesのutilsで提供されていた
MergeInsertionsという名前で定義されてるけど、
Intersectionのほうが良くない?という気がするので、タイトルはそちらにしてるmrsekut.icon